# -*- coding: utf-8 -*-
import os
import sys
import threading
import time
from pathlib import Path

from PySide2.QtWidgets import *

from TESS_PythonAPI.Tessng import TessngFactory
from tess_local_simulator.CustomPlugin import CustomPlugin
from httpserver.utils import get_device_id
import httpserver.utils

def start_local_tess_simulator(config_dict: dict):
    # threading.Thread(target=get_device_id, args=(config_dict['commandChannel'],)).start()
    # threading.Thread(target=get_device_id, args=(config_dict['commandChannel'],)).start()
    #
    # while True:
    #     if httpserver.utils.deviceId:
    #         print("httpserver.utils.deviceId",httpserver.utils.deviceId)
    #         config_dict['deviceId'] = httpserver.utils.deviceId
    #     time.sleep(0.1)

    app = QApplication()

    workspace = os.fspath(Path(__file__).resolve().parent)
    config = {'__workspace': workspace,
              '__netfilepath': config_dict['netPath'],
              '__simuafterload': True,
              '__custsimubysteps': True
              }
    plugin = CustomPlugin(config_dict)
    factory = TessngFactory()
    tessng = factory.build(plugin, config)
    if tessng is None:
        sys.exit(0)
    else:
        sys.exit(app.exec_())
